package main.helper
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import iris.flex.logic.AdvancedPopUpManager;
	import iris.flex.logic.FlexProcessor;
	
	import main.view.ViewCharacter;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.events.FlexEvent;
	
	import orm.iris.dao.TblCharacter_DAO;
	import orm.iris.vo.TblCharacterVO;
	
	import sub.event.CharInfoEvent;
	import sub.event.CharacterEvent;
	import sub.view.CharInfo;
	import sub.view.Character;
	
	public class ViewCharacterHelper
	{
		public function ViewCharacterHelper()
		{
			if(_instance != null)
				throw new Error("Singleton!");
			_instance = this;
		}
		// 实例
		private static var _instance:ViewCharacterHelper;
		
		public static function getInstance():ViewCharacterHelper
		{
			if(_instance == null)
				new ViewCharacterHelper();
			return _instance;
		}
		// ↑↑↑ Singleton ↑↑↑
		private var _view:ViewCharacter;
		
		public function creationCompleteListener(event:FlexEvent):void
		{
			this._view = event.currentTarget as ViewCharacter;
			
			_view.tabChar.addEventListener(Event.CHANGE, tabCharChange);
			_view.addEventListener(CharacterEvent.CHARACTER_EVENT_DELETE, charDeleteListener);
			// 查询现存数据
			TblCharacter_DAO.getInstance().getTableContent(getTblCharacterResult);
		}
		private function getTblCharacterResult(result:ArrayCollection):void
		{
			if(result.length == 0)
			{
				popUpCharInfo();
			}
			else
			{
				_remainTblCharacters = result.source;
				addCharInfoQuene();
			}
		}
		
		private var _tabCharPrevSelectedIndex:int;
		public function tabCharChange(event:Event):void
		{
			// 最后一个被选中，“+”
			if(_view.tabChar.selectedChild is Canvas)
				popUpCharInfo();
			else
				_tabCharPrevSelectedIndex = _view.tabChar.selectedIndex;
		}
		
		private var _popUpManager:AdvancedPopUpManager;
		private function popUpCharInfo():void
		{
			_popUpManager = new AdvancedPopUpManager(
				CharInfo, true, _view.tabChar, AdvancedPopUpManager.LOCATE_PARENT_CENTER, AdvancedPopUpManager.EFFECT_BLUR);

			var charInfo:CharInfo = _popUpManager.createPopUp() as CharInfo;
			charInfo.addEventListener(CharInfoEvent.CHAR_INFO_EVENT_FINISH, charInfoFinish);
			charInfo.addEventListener(CharInfoEvent.CHAR_INFO_EVENT_CANCEL, charInfoCancel);
		}
		/**
		 * 确定增加角色
		 */
		private function charInfoFinish(event:CharInfoEvent):void
		{
			_popUpManager.removePopUp();
			
			// 将角色插入数据库
			var vo:TblCharacterVO = new TblCharacterVO();
			vo.prop_class = event.propClass;
			vo.prop_talent = event.propTalent;
			TblCharacter_DAO.getInstance().insertRow(vo);
			// 将角色加入画面
			_remainTblCharacters = [vo];
			addCharInfoQuene();
		}
		/**
		 * 取消增加角色
		 */
		private function charInfoCancel(event:CharInfoEvent):void
		{
			_popUpManager.removePopUp();
			
			_view.tabChar.selectedIndex = _tabCharPrevSelectedIndex;
		}
		/**
		 * 增加角色
		 */
		private var _remainTblCharacters:Array;
		private function addCharInfoQuene():void
		{
			if(_remainTblCharacters.length == 0)
				return;
				
			var vo:TblCharacterVO = _remainTblCharacters.shift();			
			var char:Character = _view.tabChar.addChildAt(new Character(), _view.tabChar.numChildren - 1) as Character;
			// 设置初始值
			char.helper.setTblCharacterVo(vo);
			// 追加角色标签
			_view.tabChar.removeEventListener(Event.CHANGE, tabCharChange);
			_view.tabChar.addEventListener(FlexEvent.UPDATE_COMPLETE, tabCharUpdateComplete);
		}
		/**
		 * 增加角色后处理
		 */
		private function tabCharUpdateComplete(event:FlexEvent):void
		{
			_view.tabChar.removeEventListener(FlexEvent.UPDATE_COMPLETE, tabCharUpdateComplete);
			_view.tabChar.addEventListener(Event.CHANGE, tabCharChange);
			// 选中角色标签
			_view.tabChar.selectedIndex = _view.tabChar.numChildren - 2;
			// 剩余角色
			FlexProcessor.extCallLater(addCharInfoQuene);
		}
		/**
		 * 删除角色
		 */
		private function charDeleteListener(event:CharacterEvent):void
		{
			_view.tabChar.removeChild(event.target as DisplayObject);
			// 删除数据库
			TblCharacter_DAO.getInstance().deleteRow(event.tblCharacterVo);
			// 删除画面
			_view.tabChar.removeEventListener(Event.CHANGE, tabCharChange);
			_view.tabChar.addEventListener(FlexEvent.UPDATE_COMPLETE, tabCharUpdateComplete);
			
		}
	}
}